package ru.exigenservices.intern.webapp.action;

import ru.exigenservices.intern.model.Contact;
import ru.exigenservices.intern.service.AddressBookService;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

public class ContactForm {
    public Long getId() {
        return contact.getId();
    }
    
    private void setId(Long id) {
        contact = addressBookService.getContact(id);       
    }

    public String getTitle() {
        return contact.getTitle();
    }

    public void setTitle(String title) {
        contact.setTitle(title);
    }

    public String getPhoneNumber() {
        return contact.getPhoneNumber();
    }

    public void setPhoneNumber(String phoneNumber) {
        contact.setPhoneNumber(phoneNumber);
    }

    public String edit() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long id = Long.valueOf(request.getParameter("id"));
        setId(id);
        return "edit";
    }

    public String save() {
        addressBookService.saveContact(contact);
        return "index";
    }

    public String delete() {
        addressBookService.deleteContact(contact);
        return "index";
    }

    public void setAddressBookService(AddressBookService addressBookService) {
        this.addressBookService = addressBookService;
    }

    private AddressBookService addressBookService;

    private Contact contact;
}
